package com.gordon.tips.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.gordon.tips.domain.Reply;
import com.gordon.tips.domain.Topic;
import com.gordon.tips.domain.User;
import com.gordon.tips.log.Logger;
import com.gordon.tips.log.LoggerImpl;
import com.gordon.tips.service.ReplyService;
import com.gordon.tips.service.TopicService;
import com.gordon.tips.util.Constants;
import com.gordon.tips.util.Page;

/**
 *****************************************************************************
 ** Module : Topic control module
 ** 
 ** Date:  Aug 14, 2011
 ** 
 ** Author: Gordon
 ** 
 ** Email: 462991284@qq.com
 ** 
 ** (C) Copyright 2011 Gordon's Office
 *****************************************************************************
 */
public class TopicAction extends BaseAction {
	
	private TopicService topicService;
	public ReplyService replyService;
	private List<Topic>topics;
	private Topic topic;
	private List<Reply>replies;
	private Reply reply;
	
	private int id;
	private int pageIndex;
	private Page page;
	
	
	private static Logger logger = new LoggerImpl(TopicAction.class);
	
	/**
	 * Page the topic list.
	 * @return
	 * @Author: Gordon
	 */
	public String gotoPage()
	{
		Page page=new Page(topicService.countAllTopics(),10,pageIndex+"");
		topics=topicService.getTopic(page.getStart(), 10);
		
		//package to json type
		StringBuffer sb=new StringBuffer();
		sb.append("{\"data\":[");
		for(Topic t:topics)
		{
			sb.append("{");
			sb.append("\"id\":"+"\""+t.getId()+"\",");
			sb.append("\"top\":"+"\""+t.getTop()+"\",");
			sb.append("\"title\":"+"\""+t.getTitle()+"\",");
			sb.append("\"authorName\":"+"\""+t.getAuthor().getName()+"\",");
			sb.append("\"createTime\":"+"\""+t.getCreateTime()+"\",");
			sb.append("\"reply\":"+"\""+t.getReply()+"\",");
			sb.append("\"view\":"+"\""+t.getView()+"\",");
			sb.append("\"lastReplyerName\":"+"\""+t.getLastReplyer().getName()+"\",");
			sb.append("\"lastReplyTime\":"+"\""+t.getLastReplyTime()+"\"},");
		}
		sb.deleteCharAt(sb.length()-1);		
		sb.append("]}");

		PrintWriter out = null;
		try {
			out = response.getWriter();
			logger.debug("data="+sb.toString());
			out.print(sb.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			out.close();
		}
		return null;
	}
	
	/**
	 * Get one topic
	 * @param id
	 * @return
	 * @Author: Gordon
	 */
	public String getTopicById()
	{				
		topic=topicService.getTopicById(id);
		page=new Page(topic.getReply()+1,10,pageIndex+"");
		replies=replyService.getRepliesByTopicId(id,page.getStart());
		return "showATopic";
	}
	
	/**
	 * add a new topic
	 * @return
	 * @Author: Gordon
	 */
	public String addNewTopic()
	{
		topic.setCreateTime(new Date());
		topic.setLastReplyTime(new Date());
		
		//add score
		User user=(User) session.get("user_session");
		user.setScore(user.getScore()+Constants.SCORE_ADD_NEWTOPIC);
		session.put("user_session", user);
		
		topicService.addNewTopic(topic, reply, user);
		return "forum";
	}
	
	/**
	 * Update the topic 
	 * @return
	 * @Author: Gordon
	 */
	public String updateTopic()
	{
		//Validate the topic is posted by the user in session
		Topic topic2=topicService.getTopicById(topic.getId());
		User user=(User) session.get("user_session");
		if(user==null)return "ilegalAccess";
		if(topic2.getAuthor().getId()!=user.getId())return "ilegalAccess";
		
		PrintWriter out = null;
		topicService.editTopic(topic, reply);		
		
		try {
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.println("OK");
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
		finally
		{
			out.close();
		}
		return null;
	}
	

	public TopicService getTopicService() {
		return topicService;
	}

	public void setTopicService(TopicService topicService) {
		this.topicService = topicService;
	}

	public ReplyService getReplyService() {
		return replyService;
	}

	public void setReplyService(ReplyService replyService) {
		this.replyService = replyService;
	}

	public List<Topic> getTopics() {
		return topics;
	}

	public void setTopics(List<Topic> topics) {
		this.topics = topics;
	}

	public int getPageIndex() {
		return pageIndex;
	}

	public void setPageIndex(int pageIndex) {
		this.pageIndex = pageIndex;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public List<Reply> getReplies() {
		return replies;
	}

	public void setReplies(List<Reply> replies) {
		this.replies = replies;
	}

	public Reply getReply() {
		return reply;
	}

	public void setReply(Reply reply) {
		this.reply = reply;
	}
	
	
	
	
	
	

}
